"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import urllib
import wx
import webbrowser


UC_TREE = 6000
UC_GO = 6001
UC_NO = 6002


def get_latest_version(current_version):
    try:
        handle = urllib.urlopen('http://files.iualdii.net/GSB/version.txt')
        lines = handle.readlines()
        if len(lines) < 2: return current_version, []
        new_version = int(lines[0])
        changelog = lines[1:]
        return new_version, changelog
    except:
        return current_version, []


def parse_changelog(changelog):
    new = []
    fix = []
    other = []
    
    for item in changelog:
        item_type = item[0]
        item_text = item[2:].strip()
        
        if len(item_text) == 0: continue
        
        if item_type == '+':
            new.append(item_text)
        elif item_type == '!':
            fix.append(item_text)
        else:
            other.append(item_text)
    
    return {'new':new, 'fix':fix, 'other':other}


class UpdateDisplay(wx.Panel):
    def __init__(self, parent, version, changelog):
        wx.Panel.__init__(self, parent)
        self.parent = parent
        self.version = version
        
        content = wx.BoxSizer(wx.VERTICAL)
        tree = wx.TreeCtrl(self, UC_TREE, style = wx.TR_HAS_BUTTONS | wx.TR_LINES_AT_ROOT | wx.BORDER_SUNKEN | wx.TR_HAS_VARIABLE_ROW_HEIGHT)
        root = tree.AddRoot(str(version))
        if len(changelog['new']) > 0:
            new = tree.AppendItem(root, 'New features')
            for item in changelog['new']:
                item = '* ' + item
                item = self.wrap_item_text(item)
                tree.AppendItem(new, item)
        if len(changelog['fix']) > 0:
            fix = tree.AppendItem(root, 'Fixed bugs')
            for item in changelog['fix']:
                item = '* ' + item
                item = self.wrap_item_text(item)
                tree.AppendItem(fix, item)
        if len(changelog['other']) > 0:
            other = tree.AppendItem(root, 'Other changes')
            for item in changelog['other']:
                item = '* ' + item
                item = self.wrap_item_text(item)
                tree.AppendItem(other, item)
        
        tree.Expand(root)
        
        buttons = wx.BoxSizer(wx.HORIZONTAL)
        go = wx.Button(self, UC_GO, 'Go to site')
        no = wx.Button(self, UC_NO, 'Continue')
        
        buttons.Add(go, 1, wx.EXPAND)
        buttons.Add((0, 0), 1, wx.EXPAND)
        buttons.Add(no, 1, wx.EXPAND)
        
        content.Add(tree, 5, wx.EXPAND | wx.ALL, 10)
        content.Add(buttons, 0, wx.EXPAND | wx.ALL, 10)
        
        self.Bind(wx.EVT_BUTTON, self.close, id=UC_NO)
        self.Bind(wx.EVT_BUTTON, self.launch_browser, id=UC_GO)
        
        self.SetSizer(content)
    
    
    def wrap_item_text(self, text):
        if len(text) < 45: return text
        split_point = text.rfind(' ', 0, 45)
        return text[:split_point] + ' \n\t' + self.wrap_item_text(text[split_point+1:])
    
    
    def close(self, event):
        self.parent.close(None)
    
    
    def launch_browser(self, event):
        webbrowser.open('http://gsbedit.iualdii.net/download')
        self.close(None)


class UpdateDisplayWindow(wx.Frame):
    def __init__(self, parent, version, changelog, close_callback):
        wx.Frame.__init__(self, parent, -1, 'Update available', style=wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.RESIZE_BOX | wx.MAXIMIZE_BOX))

        self.parent = parent
        self.close_callback = close_callback
        
        self.Bind(wx.EVT_CLOSE, self.close)
        
        self.panel = UpdateDisplay(self, version, changelog)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel, 1, wx.EXPAND)

        self.SetSizer(sizer)
        
        self.Center()
        self.Show(True)
        self.Raise()
    
    
    def close(self, event):
        self.close_callback()
        self.Destroy()
